package com.itheima.sfbx.insurance.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.sfbx.framework.commons.exception.ProjectException;
import com.itheima.sfbx.framework.commons.utils.BeanConv;
import com.itheima.sfbx.insurance.dto.InsuranceConditionVO;
import com.itheima.sfbx.insurance.enums.InsuranceConditionEnum;
import com.itheima.sfbx.insurance.mapper.InsuranceConditionMapper;
import com.itheima.sfbx.insurance.pojo.InsuranceCondition;
import com.itheima.sfbx.insurance.service.IInsuranceConditionService;
import com.itheima.sfbx.insurance.service.IInsuranceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description：保险筛选项服务实现类
 */
@Slf4j
@Service
public class InsuranceConditionServiceImpl extends ServiceImpl<InsuranceConditionMapper, InsuranceCondition> implements IInsuranceConditionService {

    @Autowired
    private IInsuranceService insuranceService;


    @Override
    @Transactional
    public Boolean save(List<InsuranceConditionVO> insuranceConditionListVOs) {
        List<InsuranceCondition> beanList = BeanConv.toBeanList(insuranceConditionListVOs, InsuranceCondition.class);
        if (CollUtil.isEmpty(beanList)){
            throw new ProjectException(InsuranceConditionEnum.SAVE_FAIL);
        }
        return this.saveBatch(beanList);
    }

    @Override
    @Transactional
    public Boolean update(List<InsuranceConditionVO> insuranceConditionVOs) {
        List<InsuranceCondition> beanList = BeanConv.toBeanList(insuranceConditionVOs, InsuranceCondition.class);
        if (CollUtil.isEmpty(beanList)){
            throw new ProjectException(InsuranceConditionEnum.UPDATE_FAIL);
        }
        return this.updateBatchById(beanList);
    }


    @Override
    public List<InsuranceConditionVO> findList(InsuranceConditionVO insuranceConditionVO) {
        List<InsuranceCondition> insuranceConditionList = this.lambdaQuery()
                .in(InsuranceCondition::getInsuranceId, insuranceConditionVO.getInsuranceIds())
                .list();
        if (CollUtil.isEmpty(insuranceConditionList)){
            return new ArrayList<InsuranceConditionVO>();
        }
        List<InsuranceConditionVO> beanList = BeanConv.toBeanList(insuranceConditionList, InsuranceConditionVO.class);
        return beanList;
    }

}
